/**
 *  MenuPanel class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 * @version 0.7.2
 */

/*
 * DO NOT USE THIS CLASS ON YOUR OWN.
 * Modified version fo Ext.menu.Menu.
 * This is a specialized implementation to make the menu
 * hostable inside a panel
 */
Ext.ux.MenuPanelMenu = Ext.extend(Ext.menu.Menu, {

    initComponent : function(){
        Ext.apply(this,{
            floating:false,
            hidden:false
        });
        Ext.ux.MenuPanelMenu.superclass.initComponent.call(this);
        Ext.menu.MenuMgr.unregister(this);
    }

});

Ext.reg('menupanelmenu', Ext.ux.MenuPanelMenu);

Ext.ux.MenuPanel = Ext.extend(Ext.Panel,{

    /**
     * @cfg {Boolean} trackSelection remembers the selected menu when set to true (default false)
     */
    trackSelection:false,

    /**
     * @cfg {Number} selectedIndex sets the initial selected menu item
     */
    selectedIndex:-1,

    initComponent:function()
    {
        Ext.apply(this,{
            deferLayout:true
        });
        Ext.ux.MenuPanel.superclass.initComponent.call(this);
        if(this.menu)
        {
            this.menu = Ext.menu.MenuMgr.get(this.menu);
            this.add(this.menu);
            this.menu = this.items.get(0);
            this.menu.setSize('auto','100%');
            this.menu.on('mouseout',this.onMenuMouseOut,this);
            this.menu.on('itemclick',this.onMenuItemClick,this);
            this.on('expand',this.doLayout,this);
            this.on('afterlayout',this.onAfterLayout,this)
            this.onAfterLayout();
        }
        
    },

    /**
     * selects a menu item by index
     * @param (Number) menu index to be selected
     */
    setSelectedIndex:function(index)
    {
        if(index < 0)
            this.deselectAll();

        this.selectedIndex = index;
        this.setSelection(this.menu.items.get(index), null);
    },

    /**
     * gets the selected menu item index
     * @return (Number) selected menu item index
     */
    getSelectedIndex:function()
    {
        return this.selectedIndex;
    },

    // private
    deselectAll:function()
    {
        if(this.isVisible() && !this.collapsed)
        {
            this.menu.items.each(function (item) {
                if(item.container)
                    item.container.removeClass("x-menu-item-active");
            }, this.menu);
        }
    },

    onAfterLayout:function()
    {
        this.setSelectedIndex(this.selectedIndex);
    },

    // private
    onMenuMouseOut:function(menu, event, menuItem)
    {
        if(this.menu.items.indexOf(menuItem) == this.selectedIndex)
        {
            this.setSelection(menuItem, event);
        }
        else
        {
            this.setSelectedIndex(this.selectedIndex);
        }
    },

    // private
    onMenuItemClick:function(item,event)
    {
        this.setSelectedIndex(this.menu.items.indexOf(item));
    },

    // private
    setSelection:function(item,event)
    {
        if(item != null && item.text != null && this.trackSelection)
        {
            this.deselectAll();
            if(item.container)
                item.container.addClass("x-menu-item-active");

        }
    }
});

Ext.reg('menupanel',Ext.ux.MenuPanel)